{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "553fb582",
   "metadata": {},
   "source": [
    "# 数组基础\n",
    "\n",
    "数组（Array）是NumPy中的核心类型。整个NumPy模块都是围绕数组来构建的。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "60b0b548",
   "metadata": {},
   "source": [
    "## 数组的引入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "58125d56",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "a95a6073",
   "metadata": {},
   "source": [
    "数组的全称是N维数组（N-dimensional Array，ndarray），它是一个固定大小和形状的多维容器。\n",
    "\n",
    "构造一个数组："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "939af6c1",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array([1, 2, 3, 4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "81a1e9d1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "411dd788",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array((1, 2, 3, 4))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "05732085",
   "metadata": {},
   "source": [
    "构造一个大小为2×3的二维数组："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8407815c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array([[1, 2, 3], [4, 5, 6]])"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "c23ff8a2",
   "metadata": {},
   "source": [
    "列表不支持直接用加法将每个元素都加1的操作："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3c5a56fd",
   "metadata": {},
   "outputs": [],
   "source": [
    "b = [1, 2, 3, 4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "48d91674",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "can only concatenate list (not \"int\") to list",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[0;32mIn [7]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mb\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\n",
      "\u001b[0;31mTypeError\u001b[0m: can only concatenate list (not \"int\") to list"
     ]
    }
   ],
   "source": [
    "b + 1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7ffc8360",
   "metadata": {},
   "source": [
    "但是数组可以："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "de50ac3f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 3, 4, 5])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a + 1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "23d7d5cc",
   "metadata": {},
   "source": [
    "数组的加法和乘法与列表不一样："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "f264d18e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 4, 6, 8])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a + a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "f56f2bd8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 4, 6, 8])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a * 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "96cd5852",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  4,  9, 16])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a * a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "cccb73b3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 1., 1., 1.])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a / a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "506d1863",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "c66ffce1",
   "metadata": {},
   "source": [
    "## 数组的属性\n",
    "\n",
    "数组有一些基础属性，例如形状："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "b9b71377",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4,)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a22da511",
   "metadata": {},
   "source": [
    "也可以用函数`np.shape()`获取："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "15be1271",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4,)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.shape(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5e0f4703",
   "metadata": {},
   "source": [
    "该函数会将输入先转换为数组，因此还可以作用在非数组上："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "bb78156c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4,)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.shape([1, 2, 3, 4])"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "ef9504ea",
   "metadata": {},
   "source": [
    "多维列表的形状："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "69dfc41e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 3)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.shape([[1, 2, 3], [4, 5, 6]])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3ee8f998",
   "metadata": {},
   "source": [
    "数组有类型的概念，数组中的元素属于同一个类型："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "6363789d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('int64')"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.dtype"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "2b8399c8",
   "metadata": {},
   "source": [
    "每个元素所占的字节数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "541c6128",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.itemsize"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "b7c35f32",
   "metadata": {},
   "source": [
    "数组中的元素总数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "4853c8bb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.size"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "26c396d4",
   "metadata": {},
   "source": [
    "数组使用一段连续的内存存储数据，数组所有元素所占空间："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "bc495b2d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "32"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.nbytes"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "7acaf2d9",
   "metadata": {},
   "source": [
    "属性`.nbytes`的值是这段连续内存的大小，等于属性`.itemsize`和属性`.size`的乘积。事实上，数组所占的存储空间要比这个数字大，因为数组还需要额外的空间存储数组的形状和数据类型信息。\n",
    "\n",
    "查看数组维度："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "3bbc2292",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.ndim"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "52d6348d",
   "metadata": {},
   "source": [
    "## 数组的类型\n",
    "\n",
    "数组的类型在产生数组时确定："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "7db40b27",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "4a09e75c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('int64')"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.dtype"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "e8e51f32",
   "metadata": {},
   "source": [
    "浮点数的类型默认是64位浮点数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "e6e905cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "b = np.array([1.2, 3.5, 5.1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "bedff498",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('float64')"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.dtype"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "d59dd47d",
   "metadata": {},
   "source": [
    "当传入数据中有多种类型时，NumPy会自动进行判断，将数组转换为最通用的类型："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "122375d0",
   "metadata": {},
   "outputs": [],
   "source": [
    "c = np.array([1 + 2j, 4.5, 3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "f456e68d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('complex128')"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c.dtype"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "28de4555",
   "metadata": {},
   "source": [
    "更复杂的输入类型组合，NumPy会将它们转化为最基本的类型：object："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "e63b35f1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['abc', 1, {1, 2, 3}], dtype=object)"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array(['abc', 1, {1, 2, 3}])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f94c611d",
   "metadata": {},
   "source": [
    "可以在定义数组的时候，指定dtype："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "60f88577",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array([1, 2, 3, 4], dtype=float)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "43fc8c6d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('float64')"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.dtype"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "71938589",
   "metadata": {},
   "source": [
    "dtype可以接受多种类型的参数：\n",
    "- Python中的类型名如：int、float、complex、str等。\n",
    "- NumPy中的类型名：np.float32、np.int64、np.uint等。\n",
    "- 类型名对应的字符串，如：\"int\"、\"float\"、\"float32\"、\"int64\"、\"str\"等。\n",
    "\n",
    "数组的类型还可以转换："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "c40e4d43",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.asarray(a, dtype=int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "ca8091be",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.astype(int)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6986ea76",
   "metadata": {},
   "source": [
    "原数组不会变化："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "a48da9c1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 2., 3., 4.])"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bea8f795",
   "metadata": {},
   "source": [
    "## NumPy中的特殊值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "10846888",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/var/folders/jy/yvmv88r909s2y0qzjw2ryn7m0000gn/T/ipykernel_20111/2604490398.py:1: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  np.array([1, -1, 0]) / 0\n",
      "/var/folders/jy/yvmv88r909s2y0qzjw2ryn7m0000gn/T/ipykernel_20111/2604490398.py:1: RuntimeWarning: invalid value encountered in true_divide\n",
      "  np.array([1, -1, 0]) / 0\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([ inf, -inf,  nan])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array([1, -1, 0]) / 0"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "59e84f4f",
   "metadata": {},
   "source": [
    "## 数组的生成"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3cba6e06",
   "metadata": {},
   "source": [
    "全0数组和全1数组："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "3a78a3e9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.zeros(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "c963432e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 0., 0.],\n",
       "       [0., 0., 0.]])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.zeros((2, 3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "e87fde79",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1.],\n",
       "       [1., 1., 1.]])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ones((2, 3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "3d6e8393",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ones(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ff06eeea",
   "metadata": {},
   "source": [
    "指定间距的等距数组："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "8af840be",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "086f4370",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0., 1., 2., 3., 4.])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(5.0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "4e4d7b5b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(0, 1, 0.1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dae71d88",
   "metadata": {},
   "source": [
    "指定大小的等距数组："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "41ea485a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1.        ,  3.11111111,  5.22222222,  7.33333333,  9.44444444,\n",
       "       11.55555556, 13.66666667, 15.77777778, 17.88888889, 20.        ])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linspace(1, 20, 10)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "dc8e18db",
   "metadata": {},
   "source": [
    "## 数组的索引"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "39f2b5b6",
   "metadata": {},
   "source": [
    "一维数组的单元素索引："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "469f6cd1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 2., 3., 4.])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "d612e960",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "e13f0454",
   "metadata": {},
   "outputs": [],
   "source": [
    "a[0] = 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "352ac9eb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([10.,  2.,  3.,  4.])"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cad55018",
   "metadata": {},
   "source": [
    "切片："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "f11d6694",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2., 3.])"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[1:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "ada35652",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 4.,  3.,  2., 10.])"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[::-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "81064825",
   "metadata": {},
   "source": [
    "多维数组的索引："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "ab747201",
   "metadata": {},
   "outputs": [],
   "source": [
    "b = np.array([[ 1,  2,  3,  4,  5],\n",
    "              [11, 12, 13, 14, 15],\n",
    "              [21, 22, 23, 24, 25],\n",
    "              [31, 32, 33, 34, 35],\n",
    "              [41, 42, 43, 44, 45],\n",
    "              [51, 52, 53, 54, 55]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "009da3f4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(6, 5)"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "93b3dc9f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[0][1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "f692d239",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[0, 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "fd087fcc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "55"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[-1, -1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2643c626",
   "metadata": {},
   "source": [
    "索引某一行或某一列："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "cd237b87",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 2, 12, 22, 32, 42, 52])"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[:, 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "4736523d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([11, 12, 13, 14, 15])"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "8a9e83b0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([11, 12, 13, 14, 15])"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[1, :]"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "a36cdee5",
   "metadata": {},
   "source": [
    "索引其中的某几行和某几列，例如，第二三行的第三四列："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "f3216a3e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[13, 14],\n",
       "       [23, 24]])"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[1:3, 2:4]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fbb63edd",
   "metadata": {},
   "source": [
    "## 数组的迭代"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "46687e2e",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.arange(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "aef4d165",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "for i in a:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "dbacdb16",
   "metadata": {},
   "outputs": [],
   "source": [
    "b = np.arange(24)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "f468ed74",
   "metadata": {},
   "outputs": [],
   "source": [
    "b.shape = 4, 6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "5864cf09",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3,  4,  5],\n",
       "       [ 6,  7,  8,  9, 10, 11],\n",
       "       [12, 13, 14, 15, 16, 17],\n",
       "       [18, 19, 20, 21, 22, 23]])"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "b3c4e978",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4 5]\n",
      "[ 6  7  8  9 10 11]\n",
      "[12 13 14 15 16 17]\n",
      "[18 19 20 21 22 23]\n"
     ]
    }
   ],
   "source": [
    "for i in b:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "a8b63ae1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0, 0) 0\n",
      "(0, 1) 1\n",
      "(0, 2) 2\n",
      "(0, 3) 3\n",
      "(0, 4) 4\n",
      "(0, 5) 5\n",
      "(1, 0) 6\n",
      "(1, 1) 7\n",
      "(1, 2) 8\n",
      "(1, 3) 9\n",
      "(1, 4) 10\n",
      "(1, 5) 11\n",
      "(2, 0) 12\n",
      "(2, 1) 13\n",
      "(2, 2) 14\n",
      "(2, 3) 15\n",
      "(2, 4) 16\n",
      "(2, 5) 17\n",
      "(3, 0) 18\n",
      "(3, 1) 19\n",
      "(3, 2) 20\n",
      "(3, 3) 21\n",
      "(3, 4) 22\n",
      "(3, 5) 23\n"
     ]
    }
   ],
   "source": [
    "for idx, v in np.ndenumerate(b):\n",
    "    print(idx, v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "34e14193",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
